<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2020/07/27
 * Time: 下午5:13
 */
namespace App\Api\Controllers\CmbcUnion;

use App\Api\Controllers\BaseController;
use App\Common\PaySuccessAction;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use App\Api\Controllers\CmbcUnion\BaseController as base;

class NotifyController extends BaseController{

    public function pay_notify_url(Request $request){
        try {
            $data = $request->all();
            $base = base::getInstance();
            $ret = json_decode($base->Sm2Decrypt($data['data']), true);
            Log::channel("cmbcunion")->info("notify", $ret);
            if (isset($ret['message']['data']['head']['respcode']) && !empty($ret['message']['data']['head']['respcode'])
            && $ret['message']['data']['head']['respcode'] == 'C000000000'
            ) {
                if (isset($ret['message']['data']['body']['extendfield']) && !empty($ret['message']['data']['body']['extendfield'])) {
                    $out_trade_no = $ret['message']['data']['body']['extendfield']; //商户订单号（原交易流水）
                    $trade_no = $ret['message']['data']['body']['chlOrderId']; //系统订单号

                    $day = date('Ymd', time());
                    $table = 'orders_' . $day;
                    if (Schema::hasTable($table)) {
                        $order = DB::table($table)->where('out_trade_no', $out_trade_no)->first();
                    } else {
                        $order = Order::where('out_trade_no', $out_trade_no)->first();
                    }

                    //订单存在
                    if ($order) {
                        //系统订单未成功

                        //单品逻辑
                        if (!DB::table('order_goods_details')->where(['out_trade_no' => $out_trade_no])->first()) {
                            \App\Common\PaySuccessAction::addCoupon($out_trade_no, $ret['message']['data']['body']['promotionDetail'], 'query');
                        }

                        if ($order->pay_status == '2') {
                            $pay_time = substr($ret['message']['data']['body']['finishtime'], 0, -3);
                            $pay_time = date('Y-m-d H:i:s', strtotime($pay_time));
                            $buyer_pay_amount = $ret['message']['data']['body']['tranamt'];
                            $remark = $ret['message']['data']['head']['tranflow'];
                            $in_data = [
                                'status' => '1',
                                'pay_status' => 1,
                                'pay_status_desc' => '支付成功',
                                'trade_no' => $trade_no,
                                'pay_time' => $pay_time,
                                'remark' => $remark,
                                'buyer_id' => '',
                                'buyer_pay_amount' => $buyer_pay_amount,
                            ];
                            $this->update_day_order($in_data, $out_trade_no);
                            if (strpos($out_trade_no, 'scan')) {

                            } else {
                                //支付成功后的动作
                                $data = [
                                    'ways_type' => $order->ways_type,
                                    'company' => $order->company,
                                    'ways_type_desc' => $order->ways_type_desc,
                                    'source_type' => '21000', //返佣来源
                                    'source_desc' => '易生支付', //返佣来源说明
                                    'total_amount' => $order->total_amount,
                                    'out_trade_no' => $order->out_trade_no,
                                    'other_no' => $order->other_no,
                                    'rate' => $order->rate,
                                    'fee_amount' => $order->fee_amount,
                                    'merchant_id' => $order->merchant_id,
                                    'store_id' => $order->store_id,
                                    'user_id' => $order->user_id,
                                    'config_id' => $order->config_id,
                                    'store_name' => $order->store_name,
                                    'ways_source' => $order->ways_source,
                                    'pay_time' => $pay_time,
                                    'device_id' => $order->device_id,
                                ];
                                PaySuccessAction::action($data);
                            }
                            echo "SUCC";
                        }
                    }
                }
            }
        } catch (\Exception $ex) {
            echo $ex->getMessage().$ex->getLine();
        }
    }


    

}
